Windows API Tutorials by Unknown

Windows API Tutorials by Unknown

Author:Unknown
Language: eng
Format: epub
Tags: comp_programming


IMalloc * SComMalloc::GetMalloc () {

IMalloc * malloc = 0;

if (CoGetMalloc (1, & malloc) == S_OK) return malloc;

else return 0;

}

That's all there is to know in order to start using Windows95 shell and its COM interfaces. Here's an example. Windows95 shell has this notion of a Desktop being the root of the "file" system. Did you notice how Windows95 applications let the user browse the file system starting at the desktop? This way you can, for instance, create files directly on your desktop, move between drives, browse a network drive, etc. It is, in effect, a poor man's Distributed File System (PMDFS). How can your application get access to PMDFS? Easy. For instance, let's write some code that will let the user pick a folder by browsing PMDFS. All we need to do is to get hold of the desktop, position ourselves with respect to it, start the built-in browser and retrieve the path that the user has chosen.

char path [MAX_PATH];

path [0] = '';

Desktop desktop;

ShPath browseRoot (desktop, unicodePath);

if (browseRoot.IsOK ()) {

FolderBrowser browser (hwnd, browseRoot, BIF_RETURNONLYFSDIRS, "Select folder of your choice");

if (browser.IsOK ()) {

strcpy (path, browser.GetPath ());

}

}

Let's start with the desktop object. It envelops the interface called IShellFolder. Notice how we conform to the First Rule of Acquisition — we allocate resources in the constructor by calling the API SHGetDesktopFolder. The smart interface pointer will take care of resource management (reference counting).

class Desktop: public SIfacePtr<IShellFolder> {

public:

Desktop () {

if (SHGetDesktopFolder (& _p) != NOERROR) throw "HGetDesktopFolder failed";

}

};

Once we have the desktop, we have to create a special kind of path that is used by PMDFS. The class ShPath encapsulates this "path." It is constructed from a regular Unicode path (use mbstowcs to convert ASCII path to Unicode: int mbstowcs(wchar_t *wchar, const char *mbchar, size_t count)). The result of the conversion is a generalized path relative to the desktop. Notice that memory for the new path is allocated by the shell--we encapsulate it in SShellPtr to make sure it is correctly deallocated.

class ShPath: public SShellPtr<ITEMIDLIST> {

public:

ShPath (SIfacePtr<IShellFolder> & folder, wchar_t * path) {

ULONG lenParsed = 0;

_hresult = folder->ParseDisplayName (0, 0, path, & lenParsed, & _p, 0);

}

bool IsOK () const { return SUCCEEDED (_hresult); }

private:

HRESULT _hresult;

};

This shell path will become the root from which the browser will start its interaction with the user.

From the client code's point of view, the browser is the path chosen by the user. That's why it inherits from SShellPtr<ITEMIDLIST>.

By the way, ITEMIDLIST is the official name for this generalized path. It is a list of, and I'm not making this up, SHITEMID's. I shall refrain from making any further comments or jokes about shell naming conventions.

class FolderBrowser: public SShellPtr<ITEMIDLIST> {

public:

FolderBrowser(HWND hwndOwner, SShellPtr<ITEMIDLIST> & root, UINT browseForWhat, char const *title);

char const * GetDisplayName () { return _displayName; }

char const * GetPath () { return _fullPath; }

bool IsOK() const { return _p != 0; };

private:

char _displayName [MAX_PATH];

char _fullPath [MAX_PATH];

BROWSEINFO _browseInfo;

};



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.